//
//  XJHNavView.swift
//  PublicPod
//
//  Created by 许俊华 on 2021/9/6.
//

import UIKit
import SnapKit

public typealias NavBtnActionBlock = () -> Void

public class XJHNavView: UIView {

    // 左侧按钮
    public lazy var navLeftBtn:XJHButton = {
        var mLeftBtn = XJHButton.buttonWithType(buttonType: .custom)
        mLeftBtn.addTarget(self, action: #selector(navLeftBtnAction), for: .touchUpInside)
        mLeftBtn.eventInterval = 1
        return mLeftBtn
    }()
    
    // 右侧按钮
    public lazy var navRightBtn:XJHButton = {
        var mRightBtn = XJHButton.buttonWithType(buttonType: .custom)
        mRightBtn.addTarget(self, action: #selector(navRightBtnAction), for: .touchUpInside)
        mRightBtn.normalTitleFont = UIFont.fontOfRegular(fontSize: 12)
        mRightBtn.normalTitleColor = UIColor.colorWithHexColor(hexadecimal: "#000000")
        return mRightBtn
    }()
    
    // 标题
    public lazy var navCenterTitleLbl:XJHLabel = {
        var mCenterTitleLbl:XJHLabel = XJHLabel.labelWithFontAndTextColor(font: UIFont.fontOfSemibold(fontSize: 18), textColor: "#000000")
        return mCenterTitleLbl
    }()
    
    // 左侧按钮标题
    private var _navLeftBtnTitle:String?
    public var navLeftBtnTitle:String {
        set {
            _navLeftBtnTitle = newValue
            _navLeftBtnTitle!.hasSuffix("png") ? navLeftBtn.setImage(UIImage.init(named:_navLeftBtnTitle!), for: UIControl.State.normal) : navLeftBtn.setTitle(_navLeftBtnTitle, for: UIControl.State.normal)
        }
        get {
            return _navLeftBtnTitle!
        }
    }
    
    // 右侧按钮标题
    private var _navRightBtnTitle:String?
    public var navRightBtnTitle:String {
        set {
            _navRightBtnTitle = newValue
            _navRightBtnTitle!.hasSuffix("png") ? navRightBtn.setImage(UIImage.init(named:_navRightBtnTitle!), for: UIControl.State.normal) : navRightBtn.setTitle(_navRightBtnTitle, for: UIControl.State.normal)
        }
        get {
            return _navRightBtnTitle!
        }
    }
    
    // 中间标题
    private var _navCenterTitle:String?
    public var navCenterTitle:String {
        set {
            _navCenterTitle = newValue
            navCenterTitleLbl.text = _navCenterTitle!
        }
        get {
            return _navCenterTitle!
        }
    }
    
    // 中间标题颜色
    private var _navCenterTitleColor:UIColor?
    public var navCenterTitleColor:UIColor {
        set {
            _navCenterTitleColor = newValue
            self.navCenterTitleLbl.textColor = _navCenterTitleColor!
        }
        get {
            return _navCenterTitleColor!
        }
    }
    
    // 右侧按钮标题颜色
    private var _navRightBtnTitleColor:UIColor?
    public var navRightBtnTitleColor:UIColor {
        set {
            _navRightBtnTitleColor = newValue
            navRightBtn .setTitleColor(_navRightBtnTitleColor!, for: .normal)
        }
        get {
            return _navRightBtnTitleColor!
        }
    }
    
    // 左侧按钮标题颜色
    private var _navLeftBtnTitleColor:UIColor?
    public var navLeftBtnTitleColor:UIColor {
        set {
            _navLeftBtnTitleColor = newValue
            navLeftBtn.setTitleColor(_navLeftBtnTitleColor!, for: .normal)
        }
        get {
            return _navLeftBtnTitleColor!
        }
    }
    
    public var navLeftBtnActionBlock:NavBtnActionBlock?
    public var navRightBtnActionBlock:NavBtnActionBlock?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        initView()
    }
    
    private func initView() -> Void {
        self.addSubview(self.navCenterTitleLbl)
        self.navCenterTitleLbl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(44)
        }

        self.addSubview(self.navLeftBtn)
        self.navLeftBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(11)
            make.bottom.equalToSuperview()
            make.height.equalTo(44)
            make.width.equalTo(30)
        }

        self.addSubview(self.navRightBtn)
        self.navRightBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-18)
            make.bottom.equalToSuperview()
            make.height.equalTo(44)
            make.width.equalTo(30)
        }
    }
    
    // MARK:左侧按钮点击
    @objc func navLeftBtnAction() -> Void {
        if self.navLeftBtnActionBlock != nil {
            navLeftBtnActionBlock!()
        }
        
    }
    
    // MARK:右侧按钮点击
    @objc func navRightBtnAction() -> Void {
        if self.navRightBtnActionBlock != nil {
            navRightBtnActionBlock!()
        }
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
